package com.screen.screendisplay.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


@Configuration
//@MapperScan("com.screen.screendisplay.mapper")
@MapperScan(
        basePackages = "com.screen.screendisplay.mapper",
        sqlSessionFactoryRef = "mybatisSqlSessionFactory"
)
public class MybatisPlusConfig {

    @Bean
    public SqlSessionFactory mybatisSqlSessionFactory(
            DataSource dataSource,
            MybatisPlusInterceptor interceptor) throws Exception {

        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath*:mapper/**/*.xml")
        );

        // 关键配置：解决类型系统问题
        factory.setTypeAliasesSuperType(Object.class);
        factory.setPlugins(interceptor);

        return factory.getObject();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


//    @Bean
//    public MybatisPlusInterceptor mybatisPlusInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//        return interceptor;
//    }

//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
////        return configuration -> configuration.setUseDeprecatedExecutor(false);
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }

//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> {
//            configuration.setUseDeprecatedExecutor(false);
//            configuration.setMapUnderscoreToCamelCase(true);
//        };
//    }

}